/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai.index;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;

import top.hiai.common.model.Article;
import top.hiai.common.model.User;
import top.hiai.common.util.RestTime;
import top.hiai.common.util.SensitiveWordsKit;
import top.hiai.config.BaseController;
import top.hiai.my.favorite.MyFavoriteService;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/

public class IndexController extends BaseController {
    IndexService indexService = new IndexService();
    
    /**
     * 渲染网站首页页面
     */
    public void index() {
    	//获取用户列表
    	setAttr("articleList", indexService.paginate(getParaToInt("p", 1), limtCount,0));
    	//获取热门文章
    	findHotArticle();
    	render("index.html");
    }

	public void download() {
		//获取用户列表
		setAttr("articleList", indexService.paginate(getParaToInt("p", 1), limtCount,1));
		//获取热门文章
		findHotArticle();
		render("download.html");
	}
    /**
     * 渲染网站文章详情页面
     */
    public void detail() {
		//获取ID
		Integer aid = getParaToInt();
		//在数据库中查找
		Article article  = indexService.getDetailArticle(aid);
		//传标题
		setAttr("seoTitle", article.get("title"));
		//文章浏览量加1
		indexService.initArticleView(aid);
		//前台传值
		setAttr("article", article);
		setAttr("articleHot", indexService.getArticleHotList());
		//获取用户评论
		setAttr("replyList", indexService.replyPaginate(getParaToInt("p", 1), limtCount,aid));
		
		setLikeAndFavoriteStatus(article);
		render("detail.html");
    }
    /**
     * 遍历热门话题，热门用户
     */
    public void findHotArticle(){
    	//热门文章
    	setAttr("articleHot", indexService.getArticleHotList());
    	//热门分类
    	setAttr("commentsHot", indexService.getCommentsHotList());
    	//热门用户
    	setAttr("userHot", indexService.getUserHotList());
    }
    
    /**
     * 如果用户已登录，则需要显示当前 article 是否已经被该用户点赞、收藏了
     */
    private void setLikeAndFavoriteStatus(Article article) {
        Ret ret = Ret.create();
        MyFavoriteService.me.setFavoriteStatus(getLoginAccount(), "article", article, ret);
        setAttr("ret", ret);
    }
    
    /**
     * 文章评论功能
     */
    public void saveArticleReply() {
		if (notLogin()) {
			renderJson(Ret.fail().set("msg", "登录后才可以评论"));
			return ;
		}
		//休息时间不能发表文章评论
		String restTimeMsg = RestTime.checkRestTime(getLoginAccount());
		if (restTimeMsg != null) {
			renderJson(Ret.fail().set("msg", restTimeMsg));
			return ;
		}
		String replyContent = getPara("replyContent");
		if (StrKit.isBlank(replyContent)) {
			renderJson(Ret.fail().set("msg", "回复内容不能为空"));
			return ;
		}
		
		if(SensitiveWordsKit.checkSensitiveWord(replyContent) != null) {
			renderJson(Ret.fail().set("msg", "回复内容不能包含敏感词"));
			return ;
		}
		//发表评论需要积分功能
//		if(!(UserRoleService.me.setCutUserPoints(UserRoleService.WEI_GUI, getLoginAccountId()))) {
//			renderJson(Ret.fail("msg", "回复文章需要10个积分，您的积分不足。"));
//			return ;
//		}
		
		Ret ret = indexService.saveArticleReply(getParaToInt("articleId"), getLoginAccountId(), replyContent);

		// 注入 nickName 与 avatar 便于 renderToString 生成 replyItem html 片段
		User loginAccount = getLoginAccount();
		ret.set("loginAccount", loginAccount);
		// 用模板引擎生成 HTML 片段 replyItem
		String replyItem = renderToString("_reply_item.html", ret);
		
		ret.set("replyItem", replyItem);
		renderJson(ret);
	}
}